/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
	@module jgrouse.widgets.Checkbox
	@requires jgrouse.widgets.InputControl
*/

jgrouse.module({
	name: 'jgrouse.widgets.Checkbox',
	requires : ['jgrouse.widgets.InputControl'],
	imports : ['jgrouse.widgets.InputControl'],
	body : function(embed)
	{
		eval(embed);
		jgrouse.widgets.registerWidget('checkbox', 'jgrouse.widgets.Checkbox');
		
		/**
			Widget implementing Checkbox. Equivalent to HTML's &lt;input type="checkbox"&gt; 
			The widget is registered under "checkbox" alias.<br>
			Exported properties:
			<ul>
				<li>modelValue - equals to checkbox's value if the box is checked, null otherwise</li>
			</ul>
			@class jgrouse.widgets.Checkbox
			@extends jgrouse.widgets.InputControl
		*/
		jgrouse.define('jgrouse.widgets.Checkbox', 'jgrouse.widgets.InputControl', function(embed)
		{
			eval(embed);
			
			return {
				/**
					@constructor initialize
					@inheritdesc
				*/
				initialize : function(options, styles)
				{
					_super(this, 'initialize', options, styles);
					this.setAttribute('type', 'checkbox');
					this.enableEvent('click');
				},
				
				/**
					Set model's value. If given value equals to checkbox's <b>value</b> property, then the 
					checkbox would become checked. 					@function setModelValue
					@param {Object} value - value to set
				*/
				setModelValue : function(value)
				{
					var v = this.getValue();
					this.setAttribute('checked', v == value);
				},
				
				/**
					Event handler for onClick - fires changed <b>modelValue</b> property depending if the box becomes 
					checked or unchecked.
					@function onClick
					@param {DOMEvent} event - event that triggered processing
					@see setModelValue
				*/
				onClick : function(event)
				{
					var c = this._domElement.checked;
					this.setAttribute('checked', c);
					this.firePropertyChange('modelValue', c? this.getValue() : null);
				}
			}
		});
	}
});		